package com.bj58.ecdata.dashboard.controllers;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.bj58.ecdata.dashboard.cache.ServiceBeanFactory;
import com.bj58.ecdata.dashboard.constant.EnumCate;
import com.bj58.ecdata.dashboard.constant.IncomeCateConst;
import com.bj58.ecdata.dashboard.constant.IncomeCityConst;
import com.bj58.ecdata.dashboard.constant.IncomeNetflowCityConst;
import com.bj58.ecdata.dashboard.constant.IncomePlatConst;
import com.bj58.ecdata.dashboard.databll.PriviledgeBLL;
import com.bj58.ecdata.dashboard.dbentity.IncomeCity;
import com.bj58.ecdata.dashboard.dbentity.IncomeSource_PC;
import com.bj58.ecdata.dashboard.service.AbstractBaseService;
import com.bj58.ecdata.dashboard.utils.MyServletRequestUtils;
import com.bj58.groupbuy.action.interceptors.Check;
import com.bj58.groupbuy.action.model.User;
import com.bj58.groupbuy.action.util.SomePublicUtil;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.BeatContext.Model;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.annotation.GET;
import com.bj58.wf.mvc.annotation.POST;
import com.bj58.wf.mvc.annotation.Path;
import com.google.common.collect.Maps;

@Path("/income")
public class IncomeController extends MvcController {
	private SomePublicUtil spu = new SomePublicUtil();
	private AbstractBaseService<IncomeCity> imCityService = ServiceBeanFactory.get(IncomeCity.class, null);
	private AbstractBaseService<IncomeSource_PC> imSourceService = ServiceBeanFactory.get(IncomeSource_PC.class, null);

	/**
	 * 收入看板-城市
	 * 
	 * @return
	 * @author zhaoxiang 2015-04-30
	 */
	@Path({ "/citycate" })
	@Check("dashboard/income/citycate")
	@GET
	@POST
	public ActionResult dashboard() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			// 检查业务线权限
			String businessOperStr = PriviledgeBLL.businessOperCheck(user);
			String[] businessOpers = businessOperStr.split(",");

			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "total");

			if ("total".equals(businessName)) {
				businessName = "zhaopin";
				cateEnum = EnumCate.valueOf(businessName);
			} else {
				cateEnum = EnumCate.valueOf(businessName);
			}

			if ("total".equals(businessOpers[0])) {
				businessOpers = Arrays.copyOfRange(businessOpers, 1, businessOpers.length);
			}
			// 目前的最大日期
			String maxDate = imCityService.getMaxDate();
			Model model = beat.getModel();
			model.add("maxDate", maxDate);
			// 业务线 中英文名
			model.add("businessName", businessName);
			model.add("businessName_CN", cateEnum.getDBName());
			model.add("business_CN_Map", EnumCate.business_CN_Map);
			// 业务线权限
			model.add("businessList", businessOpers);
			model.add("zhixiaoCityList", IncomeCityConst.ZX_CityList); // 直销城市
			if (!StringUtils.equals(businessName, "ershouche")) {
				model.add("cateList", IncomeCateConst.getCateListByBusiness(businessName)); // 重点类别，下拉选择
				model.add("defautCate", IncomeCateConst.getCateListByBusiness(businessName).get(0)); // 重点类别，下拉选择}
			}
		} catch (Exception e) {
			log.error(null, e);
			return ActionResult.view("null");
		}
		return ActionResult.view("income/cityCate");
	}

	/**
	 * 收入看板-流量
	 * 
	 * @return
	 * @author zhaoxiang 2015-04-30
	 */
	@Path({ "/netflow" })
	@Check("dashboard/income/netflow")
	@GET
	@POST
	public ActionResult netflow() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			// 检查业务线权限
			String businessOperStr = PriviledgeBLL.businessOperCheck(user);
			String[] businessOpers = businessOperStr.split(",");

			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "total");

			if ("total".equals(businessName)) {
				businessName = "zhaopin";
				cateEnum = EnumCate.valueOf(businessName);
			} else {
				cateEnum = EnumCate.valueOf(businessName);
			}

			if ("total".equals(businessOpers[0])) {
				businessOpers = Arrays.copyOfRange(businessOpers, 1, businessOpers.length);
			}
			// 目前的最大日期
			Model model = beat.getModel();
			// String maxDate = incomeCityNetflow.getMaxDate();
			// model.add("maxDate", maxDate);
			// 业务线 中英文名
			model.add("businessName", businessName);
			model.add("businessName_CN", cateEnum.getDBName());
			model.add("business_CN_Map", EnumCate.business_CN_Map);
			// 业务线权限
			model.add("businessList", businessOpers);
			model.add("zhixiaoCityList", IncomeCityConst.ZX_CityList); // 直销城市
			if (!StringUtils.equals(businessName, "ershouche")) {
				model.add("cateList", IncomeCateConst.getCateListByBusiness(businessName)); // 重点类别，下拉选择
				model.add("defautCate", IncomeCateConst.getCateListByBusiness(businessName).get(0)); // 重点类别，下拉选择}
			}
			model.add("indexList_EN", IncomeNetflowCityConst.indexList_EN); // 指标
			model.add("indexList_CN", IncomeNetflowCityConst.indexList_CN); // 中文显示指标
		} catch (Exception e) {
			log.error(null, e);
			return ActionResult.view("null");
		}
		return ActionResult.view("income/netflowCityCate");
	}

	/**
	 * 收入看板-流量来源
	 * 
	 * @return
	 * @author zhaoxiang 2015-07-06
	 */
	@Path({ "/netflowSource" })
	@Check("dashboard/income/netflowSource")
	@POST
	@GET
	public ActionResult test() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			// 检查业务线权限
			String businessOperStr = PriviledgeBLL.businessOperCheck(user);
			String[] businessOpers = businessOperStr.split(",");

			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "total");
			String platform = MyServletRequestUtils.getStringParameter(request, "platform", "PC");

			if ("total".equals(businessName)) {
				businessName = "zhaopin";
				cateEnum = EnumCate.valueOf(businessName);
			} else {
				cateEnum = EnumCate.valueOf(businessName);
			}

			if ("total".equals(businessOpers[0])) {
				businessOpers = Arrays.copyOfRange(businessOpers, 1, businessOpers.length);
			}
			// 目前的最大日期
			String maxDate = imSourceService.getMaxDate();
			
			Model model = beat.getModel();
			model.add("maxDate", maxDate);
			model.add("platform", platform);
			// 业务线 中英文名
			model.add("businessName", businessName);
			model.add("businessName_CN", cateEnum.getDBName());
			model.add("business_CN_Map", EnumCate.business_CN_Map);
			
			model.add("indexList_EN", IncomePlatConst.indexList_EN); // 指标
			model.add("indexList_CN", IncomePlatConst.indexList_CN); // 中文显示指标
			Map<String,List<?>> platSource = Maps.newHashMap();
			platSource.put("source1List_PC", IncomePlatConst.soruce1List_PC);
			platSource.put("source1List_M", IncomePlatConst.soruce1List_M);
			platSource.put("source1List_APP", IncomePlatConst.soruce1List_APP);
			model.add("sourceMap",platSource); // 指标
			// 业务线权限
			model.add("businessList", businessOpers);
		} catch (Exception e) {
			log.error(null, e);
			return ActionResult.view("null");
		}
		return ActionResult.view("income/netflowSource");
	}

	/**
	 * 分业务线kpi
	 * 
	 * @return
	 */
	@Path({ "/kpi/business" })
	@Check("dashboard/income/kpi/business")
	@GET
	@POST
	public ActionResult kpibusiness() {

		return ActionResult.view("income/kpi-business");
	}

	/**
	 * 在线产品kpi
	 * 
	 * @return
	 */
	@Path({ "/kpi/online" })
	@Check("dashboard/income/kpi/online")
	@GET
	@POST
	public ActionResult kpionline() {
		return ActionResult.view("income/kpi-online");
	}

	/**
	 * 在线产品业务增长
	 * 
	 * @return
	 */
	@Path({ "/kpi/businessup" })
	@Check("dashboard/income/kpi/businessup")
	@GET
	@POST
	public ActionResult businessup() {
		return ActionResult.view("income/kpi-businessup");
	}

}
